package com.dhms.vostok1.adapter

import android.graphics.BitmapFactory
import android.util.Base64
import android.util.Log
import android.widget.ImageView
import androidx.databinding.BindingAdapter
private const val TAG = "BindingAdapters"

@BindingAdapter("base64Image")
fun bindBase64Image(imgView: ImageView, base64String: String?) {
    base64String?.let {
        try {
            val toBeDecodedString = base64String.split("base64,")[1]
            val decodedString = Base64.decode(toBeDecodedString, Base64.DEFAULT)
            val decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.size)
            imgView.setImageBitmap(decodedByte)
        } catch (e: Exception) {
            Log.e(TAG, "Invalid base64 string: $base64String.")
        }
    }
}